// Icon.v
//
// Description:
// ------------
//
//
//
// Input(s) :
// rst, clk, pixel_row, pixel_column, Loc_X, Loc_Y, BotInfo
// Output(s):
// icon
// 
// Copyright Jabeer Ahmed and Caleb Mathisen, 2014
// 
// Created By:      Caleb Mathisen
// Author(s) :      Jabeer Ahmed, Caleb Mathisen
// Last Modified:   1/19/2014
//
// Revision History:
// -----------------
///////////////////////////////////////////////////////////////////////////

`timescale 1ns / 1ns

module Icon (
    input               rst,                // Reset
    input               clk,                // 25 MHz clock
    input       [9:0]   pixel_row,          // pixel row (from dtg)     [0-127]
    input       [9:0]   pixel_column,       // pixel col (from dtg)     [0-127]
    input       [9:0]   Loc_X,              // bot loc x (from botSIM)  [0-127]
    input       [9:0]   Loc_Y,              // bot loc y (from botSIM)  [0-127]
    input       [7:0]   BotInfo,            // bot orientation (from botSIM)

    output reg  	 [1:0]   icon = 2'b0               // icon pixel (from iconizer)
);

    wire        [10:0]  rom_addr_in;        
    wire        [1:0]   rom_data_out;

	icon_16 bot_rom (
	  .clka(clk), 			// input clka
	  .addra(rom_addr_in), 	// input  [10 : 0] addra
	  .douta(rom_data_out) 	// output [1 : 0] douta
	);

    localparam MAP_W = 512; localparam MAP_H = 512;
    localparam ICN_W = 16;  localparam ICN_H = 16;

    wire            out_of_bounds;
    wire            in_bounds_h;
    wire            in_bounds_w;    

    wire     [7:0]  Icn_X, Icn_Y;

    wire     [7:0]  Icn_X_min = 0, Icn_X_max = 0;
    wire     [7:0]  Icn_Y_min = 0, Icn_Y_max = 0;


    /* Calculate if the current pixel is out-of-bounds */
    assign  out_of_bounds = (Loc_X < 7) || (Loc_X > 505) ||         
                            (Loc_Y < 7) || (Loc_Y > 505);

    /* Calculate if the current pixel is within the width bounds of the icon */                        
    assign in_bounds_w = ((Loc_X - 7) < pixel_column) && ((Loc_X + 8) > pixel_column); 
    /* Calculate if the current pixel is height the width bounds of the icon */
    assign in_bounds_h = ((Loc_Y - 7) < pixel_row)    && ((Loc_Y + 8) > pixel_row);
    /* Calculate the icon pixel x (0-15) */
    assign Icn_X       = pixel_column - (Loc_X - 7);
    /* Calculate the icon pixel y (0-15) */
    assign Icn_Y       = pixel_row - (Loc_Y - 7);
    /* Memory location of the pixel in the rom */
    assign rom_addr_in = {BotInfo[2:0],Icn_Y[3:0],Icn_X[3:0]};
 
    always @(posedge clk or posedge rst) begin
        if (rst) begin
        end
        else if (out_of_bounds == 1'b0) begin
            if (in_bounds_w && in_bounds_h) begin
                    /* return the current pixel value */
					icon <= rom_data_out;
            end
            else begin
                    /* Or rturn transparent pixel value */
					icon <= 2'b00;
				end
		 end
	end

endmodule